package com.authority.utils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

public class FormatUtils {
	private FormatUtils() {
	}

	// private static final DecimalFormat DF1 = new DecimalFormat("0.00");
	// private static final DecimalFormat DF2 = new DecimalFormat("0.000");
	// private static final DecimalFormat DF3 = new DecimalFormat("#,##0");
	// private static final DecimalFormat DF4 = new DecimalFormat("0.0#");
	// private static final DecimalFormat DF5 = new DecimalFormat("0");
	// private static final DecimalFormat DF6 = new DecimalFormat("##%");
	// 使用DecimalFormat类对科学计数法格式的数字进行格式化
	// private static final DecimalFormat DF = new DecimalFormat("#.#####");

	private static final SimpleDateFormat SDF_YYYYMMDD = new SimpleDateFormat("yyyy-MM-dd");
	// private static final SimpleDateFormat SDF_YYYYMMDD2 = new SimpleDateFormat("yyyy/MM/dd");
	// private static final SimpleDateFormat SDF_YYYYMMDDHHMM = new SimpleDateFormat("yyyy/MM/dd HH:mm");
	// private static final SimpleDateFormat SDF_YYYYMMDDHHMM2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	// private static final SimpleDateFormat SDF_HHMM = new SimpleDateFormat("HH:mm");
	// private static final SimpleDateFormat SDF_YYYYMMDDHHMMSS = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
	private static final SimpleDateFormat SDF_YYYYMMDD_HHMMSS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	// private static final SimpleDateFormat SDF_YYYYMM = new SimpleDateFormat("yyyy-MM");

	public static enum Format {
		/** 价格格式 */
		PRICE("0.00"),
		/** 折扣格式 */
		AGIO("0.00#"),
		/** 百分比格式 */
		PERCENT_TWO_DECIMAL("0.00##%"),
		/** 百分比格式 */
		PERCENT("0%");

		private final DecimalFormat format;

		private Format(String format) {
			this.format = new DecimalFormat(format);
		}

		@SuppressWarnings("unused")
		private DecimalFormat getFormat() {
			return format;
		}
	}

	/**
	 * 将数值改为百分比
	 * 
	 * @author dan.shan
	 * @since Jul 17, 2013 2:40:22 PM
	 * 
	 * @param value
	 * @return
	 */
	// public static String getFormatValue(String value, Format format) {
	// if (SuperString.isBlank(value)) {
	// return null;
	// } else if (SuperString.isNumeric(value, SuperString.NUMERIC_DOUBLE)) {
	// double dvalue = SuperString.getDouble(value);
	// return format.getFormat().format(dvalue);
	// } else {
	// return value;
	// }
	// }

	public static String floatToPercentage(float f) {
		return String.format("%.2f%%", f * 100);
	}

	public static String null2empty(String str) {
		if (str == null) {
			return "";
		}
		return str;
	}

	public static String null2empty(Object obj) {
		if (obj == null) {
			return "";
		}
		return obj.toString();
	}

	public static synchronized Date formatString2Date(String date) throws ParseException {
		return SDF_YYYYMMDD.parse(date);
	}

	public static synchronized Date formatString2Datetime(String date) throws ParseException {
		return SDF_YYYYMMDD_HHMMSS.parse(date);
	}

	/**
	 * 将null值转化为空
	 * 
	 * @param nullVarchar
	 * @return
	 */
	public static String formatNullVarchar(String nullVarchar) {
		if (StringUtils.isEmpty(nullVarchar)) {
			return "";
		}
		nullVarchar = nullVarchar.replace("\\N", "");
		return nullVarchar;
	}
}